class ObServe{
  constructor(data){
    this.walk(data)
  }
  walk(data){
    // 1.判断data是否是对象
    if(!data || typeof data != "object"){
      return
    }
    // 遍历对象
    Object.keys(data).forEach(key=>{
      this.defineReactive(data,key,data[key])
    })
  }
  defineReactive(obj,key ,val){
    let self = this
    let dep = new Dep()
    this.walk(val)
    
    Object.defineProperty(obj,key,{
      enumerable:true,
      configurable:true,
      get(){
        Dep.target && dep.addSub(Dep.target)
        return val
      },
      set(newValue){
        if(val === newValue){
          return
        }
        val = newValue
        self.walk(newValue)
        dep.notify()
      }
    })
  }
}